/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Facades;

import DAOs.DatosclienteJpaController;
import VO.Datoscliente;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.transaction.UserTransaction;

/**
 *
 * @author Julian
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class DatosclienteBean implements DatosclienteBeanRemote {
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;

    @Resource
    private SessionContext context;

    @PostConstruct
    public void init() {
        entityManager = entityManagerFactory.createEntityManager();
    }

    @PreDestroy
    public void clear() {
        if (entityManager.isOpen()) {
            entityManager.close();

        }
    }

    public void create(Datoscliente parameter) throws Exception {
        DatosclienteJpaController dao = new DatosclienteJpaController();
        try {
            
            dao.create(parameter, entityManager);
            
        } catch (Exception ex) {
            context.setRollbackOnly();
            throw ex;
        }
    }

    public void edit(Datoscliente parameter) throws Exception {
        DatosclienteJpaController dao = new DatosclienteJpaController();
        try {

             dao.edit(parameter, entityManager);

        } catch (Exception ex) {
            context.setRollbackOnly();
            throw ex;
        }
    }

    public void destroy(String parameter) throws Exception {
        DatosclienteJpaController dao = new DatosclienteJpaController();
        try {

             dao.destroy(parameter, entityManager);

        } catch (Exception ex) {
            context.setRollbackOnly();

            throw ex;
        }
    }



    public List<Datoscliente> findall() throws Exception {
        DatosclienteJpaController dao = new DatosclienteJpaController();
        try {

            List<Datoscliente> lista = dao.findDatosclienteEntities(entityManager);

            return lista;
        } catch (Exception ex) {
            throw ex;
        }
    }

    public Datoscliente findid(String parameter) throws Exception {
        DatosclienteJpaController dao = new DatosclienteJpaController();
        try {

           Datoscliente datos =  dao.findDatoscliente(parameter, entityManager);

           return datos;
        } catch (Exception ex) {
            throw ex;
        }
    }
}
